<!doctype html>
<html>
<head>
<meta charset="utf-8">
	<title>我的订单</title>
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
	<meta content="yes" name="apple-mobile-web-app-capable">
	<meta content="black" name="apple-mobile-web-app-status-bar-style">
	<meta content="telephone=no" name="format-detection">
	<link rel="shortcut icon" href="/favicon.ico">
	<link rel="stylesheet" type="text/css" href="/H5/css/sm.min.css?v=10" />
	<link rel="stylesheet" type="text/css" href="/H5/css/reset.css?v=10" />
	<link rel="stylesheet" type="text/css" href="/H5/css/order.css?v=11" />
	<style>
	.shopping{ width:100%; padding:1% auto 0 auto; overflow:hidden; background:white; border-bottom:1px solid #eee; padding:2% 0;}
	.shopping li{ float:left;}
	.settlement{ width:100%; height:60px; border-top:1px solid #CCC; background:white; position:fixed; bottom:0; line-height:26px;max-width: 540px;}
	.settlement_red{ padding:0 5%; height:60px; background:rgb(230, 108, 166); float:right; color:white; line-height:60px;}
	.settlement_money{ font-size:.7rem; margin:4px 2% 0 2%;}
	.settlement_choice{ float:left; line-height:60px; font-size:.7rem;}
	.shopping_title{ max-height:2rem; line-height:1rem; font-size:.7rem; overflow:hidden; font-weight:bold;}
	.shopping_explain{ font-size:.6rem; color:#999; overflow: hidden;text-overflow:ellipsis;white-space: nowrap;}
	.msg{padding: 0 .6rem;background-color: white;}
	.msg div{border-bottom:1px solid #eee;}
	.msg span{display: inline-block;width: 50%;font-size: .7rem;text-indent: .6rem;height: 1.6rem;vertical-align: middle;line-height: 1.6rem;}
	.msg input{width: 100%;height:99%;}
	.msg span:last-child{text-align: right}
	</style>
</head>
<body>
	<div class="page-group ms-controller" :controller="cart">
        <div class="page page-current">
            <header class="bar bar-nav">
              	<a class="icon icon-left pull-left" href="javascript:window.history.go(-1)"></a>
              	<a class="icon icon-home pull-right" href="/H5"></a>
              	<h1 class="title">填写订单</h1>
            </header>
            <div class="content visibility">
	            <div :visible="@address" class="address">
	                <a :attr="{'href':'/H5/addresslist?id='+@params.id}" class="address_a">
	                	<dl>
	                    	<dt>{{@address.name||""+" "+@address.mobile_secure||""}}</dt>
	                    	<dd>
	                        	<div>{{@address.area_label||""+" "+@address.address||""}}</div>
	                        </dd>
	                    </dl>
	                    <div class="jiantou"></div>
	                </a>
	            </div>
	            <div :visible="!@address" style="text-align: center;">
	                <a :attr="{'href': '/H5/newaddress?jumpurl='+window.location.href}" style="line-height: 60px; color: #0894ec">
	                	点击添加收货地址
	                </a>
	            </div>
	            <img src="images/shopLine.png" style="width:100%; float:left;">
	            <ul :for="el in @list" class="shopping">
	                <li style="width:31%; margin-right:3%; padding-left: 5%"><img :attr="{src: el.list_cover || el.picture_urls[0]}" style=" width:100%" /></li>
	                <li style="width:54%;">
	                	<div class="shopping_title">{{el.title}}</div>
	                	<div class="shopping_explain">{{el.intro}}</div>
	                	<div style="color:red;">
	                	    <span style="font-size:.6rem; color:#666;">{{el.contribution>0?" 贡献:+"+el.contribution:""}}</span>
	                	</div>
	                    <div>
	                        <b style="font-size:.8rem; color:#F00;">￥{{@vip_level?el.price_1:el.sell_price}}</b>
	                        <b type="number" style="width:60px; float:right; text-align:right; color:#999">x{{el.buy_num}}</b>
	                    </div>
						<div style="font-size: .7rem;margin-top: .2rem;">{{is_null(el.spec_label):el.spec_label:""}}</div>
	                </li>
	            </ul>
	            <ul :visible="@list.length==0" class="shopping">
	                <li style="width:31%; margin-right:3%; padding-left: 5%"><img :attr="{'src': @data.list_cover || @data.picture_urls[0]}" style=" width:100%" /></li>
	                <li style="width:54%;">
	                	<div class="shopping_title" style="color: #fb6eb5">{{@data.title}}</div>
	                    <div class="shopping_explain">{{@data.intro}}</div>
	                    <div style="color:red;">
	                        <span style="font-size:.6rem; color:#666;">{{@data.contribution>0?" 贡献:+"+@data.contribution:""}}</span>
	                    </div>
	                    <div>
	                        <b style="font-size:.8rem; color:rgb(222, 66, 101);">￥{{@vip_level>0?@data.price_1:@data.sell_price}}</b>
	                        <b type="number" style="width:60px; float:right; text-align:right; color:#999">x{{@count}}</b>
	                    </div>
						<div style="font-size: .7rem;margin-top: .2rem;">{{@type}}</div>
	                </li>
	            </ul>
				<div class="msg">
					<div>
						<span>登录账号</span><span><input readonly type="text" :duplex="username" placeholder=""></span>
					</div>
				</div>
				<div class="msg">
					<div>
						<span>昵称</span><span><input  readonly type="text" :duplex="nickname" placeholder=""></span>
					</div>
				</div>
				<div class="msg">
					<div>
						<span>手机号</span><span><input id="mobile"   type="text" :duplex="phone" placeholder="请填写手机号"></span>
					</div>
				</div>

				<div class="msg">
					<div>
						<span>填写备注</span><span><input id="remark"   type="text" :duplex="remark" placeholder="请填写备注"></span>
					</div>
				</div>

				<div style="padding: .6rem 0 .6rem 5%;background-color: white;font-size:.7rem;border-bottom:1px solid #eee;">西藏新疆内蒙地区加收10元</div>
	            <div class="card card_first card_first01" style="padding-bottom: 50px;background: #f7f7f7;box-shadow: none;">
					<ul class="address_ul">
	                    <!--<li>运费<span>{{postage>0?"￥ "+postage:postage}}</span></li>-->
                        <!--<li>
	                    	邀请代付
	                        <span>
	                            <div class="item-input">
	                              <label class="label-switch">
	                                <input name="yqPay" :on-change="@change($event)" type="checkbox">
	                                <div class="checkbox"></div>
	                              </label>
	                            </div>
	                        </span>
	                    </li>-->
	                    <li :if="show">
	                    	手机号码
	                        <span style="width: 70%">
	                            <input type="text" class="input_t" :on-blur ="@setItem" :duplex="mobile" placeholder="请输入对方手机号" />
	                        </span>
	                    </li>
                    </ul>
					<div class="settlement">
					    <div class="settlement_red" :on-click="@submit">提交订单</div>
					    <div class="settlement_money">
					        合计：<b style="color:rgb(230, 108, 166); font-size:.9rem;">￥{{money.toFixed(2)}}</b>
							<span id="fee_p"  style="color:rgb(222, 66, 101);font-size: .6rem;float:right;margin-right: .5rem;display: none">含：运费10元</span>
					        <div>
					            <span :if="@con>0">贡献: +{{con}}</span>
					        </div>
					    </div>
					</div>
				</div>
            </div>
        </div>
    </div>
	<script type='text/javascript' src='/H5/js/zepto.min.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/sm.min.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/avalon.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/simpleStorage.js' charset='utf-8'></script>
	<script type='text/javascript' src='/H5/js/mall.js?v=10' charset='utf-8'></script>
	<script>
		var cart = avalon.define({
		    $id: "cart",
		    data: {picture_urls:[]},
		    list: [],
		    address: {},
		    money: 0,
            point: 0,
            dkpoint: 0,
            postage: 0,
            con: 0,
            count: 0,
		    params: Mall.parseUrl( window.location.href ),
	    	phone: "",
	    	totalPoint: 0,
	    	show: false,
	    	ph: false,
	    	rp: 0,
			username: '',
	    	mobile: '',
	    	nickname: '',
	    	remark: '',
			vip_level: 0,
			type:'',
	        change: function( e ){
	    		if( e.target.checked ){
	    			if( e.target.name == "yqPay" ){
	    				cart.show = true;
	    			}
	    			else{
	    				cart.ph = true;
	    				this.total();
	    			}
	    			$('.content').scrollTop( $('.content').height() );
	    		}
	    		else{
	    			if( e.target.name == "yqPay" ){
	    				cart.show = false;
	    			}
	    			else{
	    				cart.ph = false;
	    				this.total();
	    			}
	    		}

	        },
	        hb:function( e ){
	        	if( this.totalPoint > this.money ){
	        		if( this.rp >  this.money ){
	        			this.rp = this.money;
	        		}
	        	}
	        	else{
	        		if( this.rp >  this.totalPoint ){
	        			this.rp = this.totalPoint;
	        		}
	        	}
	        	if( this.rp < 0 ){
        			this.rp = 0;
        		}
	        	this.total();
	        },
		    getAddress: function(){
		    	Mall.request( "/api/AddressList" )
		    		.always( function( data ){
		    			if( data.error_code == 0 ){
		    				if( cart.params.addressId ){
		    					for( var i = 0; i < data.data.length; i ++ ){
		    						if( data.data[i].id == cart.params.addressId  ){
		    							cart.address = data.data[i];
		    							break;
		    						}
		    					}
		    				}
		    				else{
		    					for(  var i = 0; i < data.data.length; i ++ ){
		    						if( data.data[i].is_default == 1  ){
		    							cart.address = data.data[i];
		    							break;
		    						}
		    					}
		    				}
	    					if( !cart.address.id ){
	    						cart.address = data.data[0]
	    					}
							if(cart.address.is_remote){
								cart.money = Number(cart.money)+10;
								document.getElementById('fee_p').style.display="block";
							}
		    			}
		    		} );
		    },
		    getDetail: function(){
		    	if( cart.params.id.split(",").length > 1 ){
		    		Mall.request( "/api/ShopcartList" )
		    		    .always( function( data ){
		    		        if( data.error_code == 0 ){
								cart.vip_level = data.data.vip_level;
		    		        	var list = data.data.list,
		    		        		ids = cart.params.id.split(",");
		    		        	for( var i = 0; i < ids.length; i ++ ){
		    		        	    for( var k = 0; k < list.length; k ++ ){
		    		        	       	if( ids[i] == list[k].goods_id ){
		    		        	       		cart.list.push( list[k] );
		    		        	       	}
		    		        	    }
		    		        	}
		    		        	cart.total();
		    		        	if( cart.totalPoint > cart.money ){
		    		        		cart.rp = cart.money;
		    		        	}
		    		        	else{
		    		        		cart.rp = cart.totalPoint;
		    		        	}
		    		        	$( ".content" ).removeClass( "visibility" );
		    		        }
		    		    } )
		    	}
		    	else{
			    	Mall.request( "/api/GoodsDetail", {goods_id: cart.params.id} )
			    		.always( function( data ){
			    			if( data.error_code == 0 ){
			    				cart.data = data.data;
			    			}
			    			cart.total();
			    			if( cart.totalPoint > cart.money ){
	    		        		cart.rp = cart.money;
	    		        	}
	    		        	else{
	    		        		cart.rp = cart.totalPoint;
	    		        	}
	    		        	$( ".content" ).removeClass( "visibility" );
			    		} )
		    	}
		    },
		    total: function(){
	    		this.money = 0;
	    		this.con = 0;
	    		this.postage = 0;
		    	if( this.list.length ){
		    		for( var i = 0; i < this.list.length; i ++ ){
		    			if( this.vip_level >0 ){
		    				this.money += this.list[i].price_1*this.list[i].buy_num;
		    			}
		    			else{
		    				this.money += this.list[i].sell_price*this.list[i].buy_num;
		    			}
		    		    this.con += Math.abs(this.list[i].contribution)*this.list[i].buy_num;
		    		}
		    		if( this.money <= 100 ){
		    			this.postage = 10;
		    			this.money += this.postage;
		    		}
		    		else{
		    			if( simpleStorage.get("postage") && this.money >= simpleStorage.get("postage") ){
		    				this.postage = "包邮";
		    			}
		    			else{
		    				this.postage = 10;
		    				this.money += this.postage;
		    			}
		    		}
		    	}
		    	else{
		    		this.count = Mall.parseUrl( window.location.href ).count||1;
		    		if( this.vip_level ){
		    			this.money += this.data.price_1*this.count;
		    		}
		    		else{
		    			this.money += this.data.sell_price*this.count;
		    		}
		    		this.con = this.data.contribution*this.count;
		    		this.postage = Number( this.data.postage );
		    		this.money += this.postage;
		    	}
	    		if( $("input[name='usehp']").prop( "checked" ) ){
	    			this.money -= this.rp;
	    		}
		    },
		    submit: function(){
		    	if( Mall.LOCK ){
		    		return
		    	}
		    	if( !cart.address ){
		    		$.toast( "请填写收货地址", 2000, "warning" )
		    		return
		    	}
    	    	if( $( ".label-switch input:checked" ).attr( "name" ) != "yqPay" ){
    	    		var postData = "address_id="+cart.address.id;
    	    		if( this.list.length ){
	    	    		for( var i = 0; i < this.list.length; i ++ ){
	    	    			postData += "&goods["+this.list[i].goods_id+"]="+this.list[i].buy_num
	    	    		}
    	    		}
    	    		else{
    	    			postData += "&goods["+this.data.id+"]="+this.count
    	    		}
					if(!$("input[name='usehp']").prop( "checked" )){
						this.rp = 0;
					}
    	    		postData += "&red_packets="+this.rp;
    	    		//postData += "&mobile="+cart.mobile;
    	    		postData += "&phone="+cart.phone;
    	    		postData += "&remark="+cart.remark;
                   if(cart.type&&cart.type!='undefined'){ postData += "&spec["+cart.params.id+"]="+cart.type;console.log('我还是进来啦')}
                   for(var index = 0;index<cart.list.length;index++){
                       if(cart.list[index].spec_label){
                           postData += "&spec["+cart.list[index].goods_id+"]="+cart.list[index].spec_label;
					   }
				   }
    		    	var url = "/api/OrderSubmitBalance";
    		    	Mall.LOCK = true;
    		    	$( "#submit" ).text( "提交中" );
    		    	Mall.request( url, postData )
    		    		.always( function( data ){
    		    			if( data.error_code == 0 ){
    		    				window.history.replaceState(null,null,"/H5/pay?orderId="+data.data.order_no);
    		    				window.location.reload();
    		    			}
		    				$( "#submit" ).text( "提交订单" );
		    				Mall.LOCK = false;
    		    		} )
    	    	}
    	    	else{
    	    		var postData = "address_id="+cart.address.id+"&phone="+cart.phone+"&mobile="+cart.mobile;
    	    		if( this.list.length ){
	    	    		for( var i = 0; i < this.list.length; i ++ ){
	    	    			postData += "&goods["+this.list[i].goods_id+"]="+this.list[i].buy_num
	    	    		}
    	    		}
    	    		else{
    	    			postData += "&goods["+this.data.id+"]="+this.count;
    	    		}
					if(!$("input[name='usehp']").prop( "checked" )){
						this.rp = 0;
					}
    	    		postData += "&red_packets="+this.rp;
    	    		Mall.LOCK = true;
    	    		$( "#submit" ).text( "提交中" );
    	    		var url = "/api/OrderSubmitInvite";
    	    		$.prompt("请输入支付密码", function( value ){
    	    			postData += "&pay_password="+value;
    	    			Mall.request( url, postData )
    	    				.always( function( data ){
    	    					if( data.error_code == 0 ){
    	    						simpleStorage.set( "df", undefined );
    	    						$.toast( "提交代付成功", 1000, "success", function(){
    	    							window.location.href="/H5/friendlist?type=2";
    	    						} )
    	    					}
    	    					Mall.LOCK = false;
    	    				} )
    	    		})
    	    		$( "input.modal-text-input" ).attr( "type", "password" )
    	    	}
		    },
		    getInfo: function(){
		        if(window.location.href.indexOf('value')){
                    cart.type = decodeURI(cart.params.value);
				}
		    	Mall.request("/api/UserInfo")
		    		.always(function(data){
			    		if( data.error_code == 0 ){
			    			cart.totalPoint = data.data.red_packets;
			    			cart.phone = data.data.mobile;
			    			cart.username = data.data.username;
			    			cart.nickname = data.data.nickname;
			    			cart.vip_level = data.data.vip_level;
							if(cart.phone != ''){
								$("#mobile").attr("readOnly","true");
							}
			    		}
			    	})
		    }
		})
		cart.getInfo();
		cart.getDetail();
		cart.getAddress();
		window.onbeforeunload = function(){
			console.log( a )
			return
		};
		window.onunload = function( a ){
			console.log( a )
			return
		};
		if(cart.mobile != ''){
			$("#mobile").attr("readOnly","true");
		}
		avalon.scan( document.body );
	</script>
</body>
</html>